﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;

public partial class Main_PicAddUpdate : System.Web.UI.Page
{
    EncriptManager em = new EncriptManager();
    PicDao pd = new PicDao();
    ShopDao sd = new ShopDao();
    HouseDao hd = new HouseDao();
    DataTable dt = null;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            UsersVo uv = new UsersVo();
            uv = (UsersVo)Session["user"];
            if (uv == null)
            {
                Response.Redirect("../index.aspx");
            }
            if (uv.Ugroup != "管理员")
            {
                Response.Redirect("../index.aspx");
            }
            if (em.Decript(Request.QueryString["sid"]) != null)
            {
                ddltype.SelectedValue = "0";
                dt = sd.getAllShop();
                ddldata.DataSource = dt;
                ddldata.DataTextField = "Sname";
                ddldata.DataValueField = "Sid";
                ddldata.DataBind();
                ddldata.SelectedValue = em.Decript(Request.QueryString["sid"]);
                dt = pd.getPic(em.Decript(Request.QueryString["sid"]), "0");
                gvpic.DataSource = dt;
                gvpic.DataBind();
            }
        }
    }

    public void bd()
    {
        dt = pd.getPic(ddldata.SelectedValue, ddltype.SelectedValue);
        gvpic.DataSource = dt;
        gvpic.DataBind();
    }

    protected void ddltype_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (ddltype.SelectedValue == "0")
        {
            dt = sd.getShop("0","0","","0");
            ddldata.DataSource = dt;
            ddldata.DataTextField = "Sname";
            ddldata.DataValueField = "Sid";
            ddldata.DataBind();
        }
        if (ddltype.SelectedValue == "1")
        {
            dt = hd.getHouse("", "0", "99999", "0", "0");
            ddldata.DataSource = dt;
            ddldata.DataTextField = "Hname";
            ddldata.DataValueField = "Hid";
            ddldata.DataBind();
        }
    }
    protected void btnimgup_Click(object sender, EventArgs e)
    {
        if (ddltype.SelectedValue == "-1" && ddldata.SelectedValue == "0")
        {
            string script = "<script>alert('请选择类型和名称！');</script>";
            Page.RegisterClientScriptBlock("", script);
            return;
        }
        bool fileOK = false;
        string path = "";
        if (ddltype.SelectedValue == "0")
        {
            path = Server.MapPath("../images/shop/img/") + FileUpimg.FileName;
        }
        if (ddltype.SelectedValue == "1")
        {
            path = Server.MapPath("../images/house/img/") + FileUpimg.FileName;
        }
        if (FileUpimg.FileName != "")
        {
            if (FileUpimg.HasFile)
            {
                string fileExtension = Path.GetExtension(FileUpimg.FileName).ToLower();
                string[] allowerExtensions = { ".gif", ".jpg", ".png", ".bmp", ".jpeg" };
                for (int i = 0; i < allowerExtensions.Length; i++)
                {
                    if (fileExtension == allowerExtensions[i])
                    {
                        fileOK = true;
                    }
                }
                if (!fileOK)
                {
                    string script = "<script>alert('图片格式不正确！');</script>";
                    Page.RegisterClientScriptBlock("", script);
                    return;
                }
                else
                {
                    int number = pd.getPicNumber(ddldata.SelectedValue, ddltype.SelectedValue);
                    string picPath = path.Replace(FileUpimg.FileName, ddldata.SelectedItem.Text + "info" +(number+1).ToString()+ fileExtension);
                    FileUpimg.PostedFile.SaveAs(picPath);
                    PicVo pv = new PicVo();
                    if (ddltype.SelectedValue == "0")
                    {
                        pv.Purl = "../images/shop/img/" + ddldata.SelectedItem.Text + "info" + (number + 1).ToString() + fileExtension;
                    }
                    if (ddltype.SelectedValue == "1")
                    {
                        pv.Purl = "../images/house/img/" + ddldata.SelectedItem.Text + "info" + (number + 1).ToString() + fileExtension;
                    }
                    pv.Id = Convert.ToInt32(ddldata.SelectedValue);
                    pv.Ptype = Convert.ToInt32(ddltype.SelectedValue);
                    pv.Pname = txttitle.Text;
                    Exception ex = pd.addPic(pv);
                    if (ex == null)
                    {
                        string script = "<script>window.alert('上传添加成功！');</script>";
                        Page.RegisterClientScriptBlock("", script);
                        bd();
                    }
                    else
                    {
                        string script = "<script>window.alert('上传添加失败！');</script>";
                        Page.RegisterClientScriptBlock("", script);
                    }
                }
            }
            else
            {
                string script = "<script>alert('上传失败，格式不允许！');</script>";
                Page.RegisterClientScriptBlock("", script);
            }
        }
        else
        {
            string script = "<script>alert('上传不能为空！');</script>";
            Page.RegisterClientScriptBlock("", script);
        }
    }
    protected void gvpic_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gvpic.PageIndex = e.NewPageIndex;
        bd();
    }
    protected void gvpic_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        string id = e.CommandArgument.ToString();
        if (e.CommandName == "del")
        {
            pd.delPic(id);
        }
        bd();
    }
    protected void ddldata_SelectedIndexChanged(object sender, EventArgs e)
    {
        bd();
    }
    protected void gvpic_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Cells[2].Attributes.Add("onclick", "return confirm('确认删除吗？')");
        }
    }
}
